// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 100
      }
    ],
    "title": "商户名称",
    "label": "商户名称"
  },
  "logo": {
    "rules": [
      {
        "format": "string"
      },
      {
        "pattern": "^(https?://|/|../).+\\.(jpg|jpeg|png|webp)$"
      }
    ],
    "title": "商户LOGO",
    "label": "商户LOGO"
  },
  "description": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 1000
      }
    ],
    "title": "商户描述",
    "label": "商户描述"
  },
  "phone": {
    "rules": [
      {
        "format": "string"
      },
      {
        "pattern": "^\\d{5,20}$"
      }
    ],
    "title": "联系电话",
    "label": "联系电话"
  },
  "tags": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "商户标签",
    "label": "商户标签"
  },
  "rating": {
    "rules": [
      {
        "format": "number"
      },
      {
        "minimum": 0,
        "maximum": 5
      }
    ],
    "title": "商户评分",
    "label": "商户评分"
  },
  "status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "minimum": 0,
        "maximum": 1
      },
      {
        "range": [
          {
            "text": "关闭",
            "value": 0
          },
          {
            "text": "营业中",
            "value": 1
          }
        ]
      }
    ],
    "title": "商户状态",
    "defaultValue": 1,
    "label": "商户状态"
  }
}

const enumConverter = {
  "status_valuetotext": {
    "0": "关闭",
    "1": "营业中"
  },
  "audit_status_valuetotext": {
    "0": "待审核",
    "1": "已通过",
	"2": "已拒绝",
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
